
package com.customize.lflistview;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

public class LfListView extends ListView {
    private static final int INIT_STATE = 0;
    private static final int REFRESH_LIST = 1;
    private static final int LOAD_MORE = 2;
    private static final int NO_MORE = 3;
    private View mHeaderView;
    private View mHeaderProgress;
    private TextView mHeaderTextView;
    private View mFooterView;
    private View mFooterProgress;
    private TextView mFooterTextView;
    private OnRefreshListener mOnRefreshListener;
    private OnLoadMoreListener mOnLoadMoreListener;
    private boolean isMoreData = true;

    public LfListView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = LayoutInflater.from(getContext());
//        mHeaderView = new LinearLayout(getContext());
//        LinearLayout.LayoutParams headerParams = new LinearLayout.LayoutParams(
//                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//        headerParams.gravity = Gravity.CENTER;
//        mHeaderView.setLayoutParams(headerParams);
//        mHeaderProgress = new ProgressBar(getContext());
//        mHeaderProgress.setLayoutParams(new LayoutParams(30, 30));
//        Map<String, String> attributes = new HashMap<String, String>();
//        attributes.put("android:layout_width", "wrap_content");
//        attributes.put("android:layout_height", "wrap_content");
//        attributes.put("android:paddingTop", "10dp");
//        attributes.put("android:paddingBottom", "10dp");
//        // BridgeLayoutParamsMapAttributes params = new
//        // BridgeLayoutParamsMapAttributes(attributes);
//        mHeaderTextView = new TextView(getContext());
//        // mHeaderTextView.setLayoutParams(params);
//        ((ViewGroup) mHeaderView).addView(mHeaderProgress);
//        ((ViewGroup) mHeaderView).addView(mHeaderTextView);
        mHeaderView = inflater.inflate(R.layout.header, null);
        mHeaderProgress = mHeaderView.findViewById(R.id.header_progress);
        mHeaderTextView = (TextView) mHeaderView.findViewById(R.id.header_textview);

        mFooterView = inflater.inflate(R.layout.footer, null);
        mFooterProgress = mFooterView.findViewById(R.id.footer_progress);
        mFooterTextView = (TextView) mFooterView.findViewById(R.id.footer_textview);

        addHeaderView(mHeaderView);
        updateState(INIT_STATE);
        setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                Log.e("qinyu", "child:" + getChildCount() + "/" + totalItemCount);
                if (getFooterViewsCount() > 0 && visibleItemCount == totalItemCount) {
                    hideFooter();
                } else if (getFooterViewsCount() == 0 && visibleItemCount < totalItemCount) {
                    showFooter();
                }
            }
        });
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        if (adapter == null) {
            return;
        }
        super.setAdapter(adapter);
        mHeaderView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOnRefreshListener != null) {
                    updateState(REFRESH_LIST);
                    mOnRefreshListener.onRefreshList();
                }
            }
        });
        mFooterView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mOnLoadMoreListener != null) {
                    updateState(LOAD_MORE);
                    mOnLoadMoreListener.onLoadMore();
                }
            }
        });
        adapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                updateState(INIT_STATE);
            }
        });
    }

    public void hideFooter() {
        Log.e("qinyu", "hide footer");
        removeFooterView(mFooterView);
    }

    public void showFooter() {
        Log.e("qinyu", "show footer");
        addFooterView(mFooterView);
    }

    public void setMoreData(boolean hasMore) {
        isMoreData = hasMore;
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        mOnRefreshListener = onRefreshListener;
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        mOnLoadMoreListener = onLoadMoreListener;
    }

    public static interface OnRefreshListener {
        public void onRefreshList();
    }

    public static interface OnLoadMoreListener {
        public void onLoadMore();
    }

    private void updateState(int state) {
        switch (state) {
            case INIT_STATE:
                mHeaderProgress.setVisibility(View.GONE);
                mHeaderTextView.setText("刷新数据");
                mHeaderView.setClickable(true);
                mFooterProgress.setVisibility(View.GONE);
                if (isMoreData) {
                    mFooterTextView.setText("加载更多数据");
                    mFooterView.setClickable(true);
                } else {
                    mFooterTextView.setText("没有更多数据");
                    mFooterView.setClickable(false);
                }
                break;
            case REFRESH_LIST:
                mHeaderProgress.setVisibility(View.VISIBLE);
                mHeaderTextView.setText("数据刷新中...");
                mHeaderView.setClickable(false);
                mFooterView.setClickable(false);
                break;
            case LOAD_MORE:
                mFooterProgress.setVisibility(View.VISIBLE);
                mFooterTextView.setText("数据加载中...");
                mHeaderView.setClickable(false);
                mFooterView.setClickable(false);
                break;
            default:
                break;

        }
    }
}
